package dom;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.Iterator;

public class XMLStudentShow {
    Document doc=null;
    public void getDocument(){

        // 获取解析器
        try {
            SAXReader saxReader=new SAXReader();
            doc= saxReader.read(new File("src/main/resources/student.xml"));
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    public void showInfo(){
        // 获取所有节点集合
        Element root=doc.getRootElement();
        for (Iterator iterable = root.elementIterator(); iterable.hasNext();){
            Element brandEle=(Element) iterable.next();

            System.out.println("学号："+brandEle.attributeValue("id"));
            for (Iterator itType=brandEle.elementIterator();itType.hasNext();){
                Element typeEle=(Element) itType.next();
                String name=typeEle.getName();
                if(name.equals("name")){
                    System.out.println("\t姓名："+typeEle.getText());
                }
                if(name.equals("course")){
                    System.out.println("\t学科："+typeEle.getText());
                }
                if(name.equals("score")){
                    System.out.println("\t分数："+typeEle.getText());
                }
            }
        }
    }

    public static void main(String[] args) {
        XMLStudentShow studentShow=new XMLStudentShow();
        studentShow.getDocument();
        studentShow.showInfo();
    }
}
